package com.app.tablet.view;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.app.tablet.R;
import com.app.tablet.data.dao.TabletDao;
import com.app.tablet.data.tasks.MainPageTask;
import com.app.tablet.notify.Notifier;

public class MainPageActivity extends BaseActivity implements Notifier, OnClickListener {
	
	private static final String TagProject = "Tablet-Project";
	private static final String TagPage = "MainPageActivity-Page ::";

	ListView reminderList;
	Button leftBtn;
	Button rightBtn;
	mycursoradaptor mycursoradap;
//	boolean[] isselected;
//	public static boolean deleteBtnBool=false;
	
	@Override
	protected void onResume() {
		super.onResume();
		startTask();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_mainpage);
		
		leftBtn=(Button)findViewById(R.id.leftHeaderBtn);
		leftBtn.setOnClickListener(this);
//		leftBtn.setVisibility(View.INVISIBLE);
		rightBtn=(Button)findViewById(R.id.rightHeaderBtn);
		rightBtn.setOnClickListener(this);
		
		reminderList = (ListView) findViewById(R.id.list_mainpage);
//		reminderList.setFocusable(true);
//		reminderList.setClickable(true);
//		reminderList.setOnItemClickListener(this);

		startTask();

		/*
		 * Cursor mainpageCursor = null;
		 * 
		 * try{ mainpageCursor =
		 * TabletDao.getTabletDao().getAllTablets(TabletDB.getInstance()); }
		 * catch(java.sql.SQLException e){ // TODO Auto-generated catch block
		 * e.printStackTrace();
		 * 
		 * } Log.i("MainPageActivity", "Cursor Value" +
		 * mainpageCursor.getCount()); Log.i("Senthil", "Curser text" +
		 * mainpageCursor.getColumnIndex(TabletDao.FIELD_TABLET_NAME));
		 * mycursoradaptor mycursoradap = new mycursoradaptor(this,
		 * mainpageCursor); reminderList.setAdapter(mycursoradap);
		 */
	}

	private void startTask() {
		showDialog(DIALOG_ID_PROGRESS);
		MainPageTask mainTask = new MainPageTask(this);
		mainTask.execute();
	}
	
	@Override
	public Object onError() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onSuccess(Object response) {
		hideProgress();
		if (response instanceof Cursor) {
			populateMainList((Cursor) response);
		}
	}

	private void populateMainList(Cursor mainpageCursor) {
		mycursoradap = new mycursoradaptor(this, mainpageCursor);
//		isselected = new boolean[mainpageCursor.getCount()];
		reminderList.setAdapter(mycursoradap);

	}

	class mycursoradaptor extends CursorAdapter {

		Context adcontext;

		public mycursoradaptor(Context context, Cursor c) {
			super(context, c);
			adcontext = context;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			TextView txtname = (TextView) view.findViewById(R.id.txt_line1);
			TextView txtroll = (TextView) view.findViewById(R.id.txt_line2);
			TextView txtdep = (TextView) view.findViewById(R.id.txt_line3);
			txtname.setText(cursor.getString(cursor.getColumnIndex(TabletDao.FIELD_TABLET_NAME)));
			txtroll.setText((cursor.getString(cursor.getColumnIndex(TabletDao.FIELD_DOSAGE))) + ", " 
					+ (cursor.getString(cursor.getColumnIndex(TabletDao.FIELD_SCHEDULE))));
			txtdep.setText(cursor.getString(cursor.getColumnIndex(TabletDao.FIELD_ALARM_TIME)));
			
//			if (isselected[cursor.getPosition()]) {
//				Log.i(TagProject, TagPage+isselected[cursor.getPosition()]);
//				AddPageActivity.dbData.setTabletname(cursor.getString(cursor.getColumnIndex(TabletDao.FIELD_TABLET_NAME)));
//				AddPageActivity.dbData.setDoasge(cursor.getString(cursor.getColumnIndex(TabletDao.FIELD_DOSAGE)));
//				AddPageActivity.dbData.setSchedule(cursor.getString(cursor.getColumnIndex(TabletDao.FIELD_SCHEDULE)));
//				AddPageActivity.dbData.setAlarmtime(cursor.getString(cursor.getColumnIndex(TabletDao.FIELD_ALARM_TIME)));
//				AddPageActivity.dbData.setTones(cursor.getString(cursor.getColumnIndex(TabletDao.FIELD_TONES)));
//				AddPageActivity.dbData.setId(cursor.getInt(cursor.getColumnIndex(TabletDao.FIELD_TABLET_ID)));
//				deleteBtnBool=true;
//				Intent intent = new Intent(MainPageActivity.this, AddPageActivity.class);
//				startActivity(intent);
//			}
			
			final Button onBtn = (Button)view.findViewById(R.id.onBtn);
			final Button offBtn = (Button)view.findViewById(R.id.offBtn);
			onBtn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					onBtn.setBackgroundColor(Color.GREEN);
					offBtn.setBackgroundColor(Color.RED);
				}
			});
			offBtn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					onBtn.setBackgroundColor(Color.RED);
					offBtn.setBackgroundColor(Color.GREEN);
				}
			});
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) adcontext.getSystemService(adcontext.LAYOUT_INFLATER_SERVICE);
			View rowview = inflater.inflate(R.layout.layout_listitem_mainpage,null);
			return rowview;
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftHeaderBtn:
			Intent in = new Intent(MainPageActivity.this, EditPageActivity.class);
			startActivity(in);
			break;
		case R.id.rightHeaderBtn:
			Intent in2 = new Intent(MainPageActivity.this, AddPageActivity.class);
			startActivity(in2);
			break;
		default:
			break;
		}
	}

//	@Override
//	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//		for (int index = 0; index < isselected.length; index++) {
//			isselected[index] = false;
//		}
//		isselected[position] = true;
//		Log.i(TagProject, TagPage+position);
//		refreshListView();
//	}
//	
//	private void refreshListView() {
//		reminderList.invalidate();
//		mycursoradap.notifyDataSetChanged();
//	}

}